package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.*;
import com.animal.mapper.AnInvitationMapper;
import com.animal.mapper.AnUserMapper;
import com.animal.response.Result;
import com.animal.service.AnInvitationService;
import com.animal.utils.UpPhotoNameUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/an-invitation")
public class AnInvitationController extends JSONAuthentication {

    @Autowired
    AnInvitationMapper anInvitationMapper;
    @Autowired
    AnInvitationService anInvitationService;
    @Autowired
    AnUserMapper anUserMapper;
    @ApiOperation(value = "添加帖子")
    @PostMapping("/add")
    public void addCar(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response,
                       @RequestParam("userId") int userId,@RequestParam("content") String content,@RequestParam("type") int type,@RequestParam("inAnState") int inAnState,@RequestParam("anPrice") float anPrice,@RequestParam("anCause") String anCause,@RequestParam("anId") int anId,@RequestParam("anTitle") String anTitle) throws  Exception{
        AnInvitation anInvitation=new AnInvitation();
        anInvitation.setUserId(userId);
        anInvitation.setContent(content);
        anInvitation.setType(type);
        anInvitation.setInAnState(inAnState);
        anInvitation.setAnPrice(anPrice);
        anInvitation.setAnCause(anCause);
        anInvitation.setAnId(anId);
        anInvitation.setAnTitle(anTitle);

        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        anInvitation.setPicture(avatar_url);
        response.reset();

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anInvitation.setInCreatTime(timestamp);

        int i=anInvitationMapper.insert(anInvitation);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "添加交流帖子")
    @PostMapping("/addjiaoliu")
    public void addCara(@RequestParam("file") MultipartFile file,HttpServletRequest request, HttpServletResponse response,
                       @RequestParam("userId") int userId,@RequestParam("content") String content,@RequestParam("type") int type,@RequestParam("anTitle") String anTitle) throws  Exception{
        AnInvitation anInvitation=new AnInvitation();
        anInvitation.setUserId(userId);
        anInvitation.setContent(content);
        anInvitation.setType(type);
        anInvitation.setAnTitle(anTitle);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        anInvitation.setPicture(avatar_url);
        response.reset();

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anInvitation.setInCreatTime(timestamp);

        int i=anInvitationMapper.insert(anInvitation);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "删除帖子")
    @PostMapping("/del")
    public void delCar(HttpServletRequest request, HttpServletResponse response, @RequestParam("inId") int inId ) throws Exception{
        int i=anInvitationMapper.deleteById(inId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改帖子内容")
    @PostMapping("/upd")
    public void updCarNum(HttpServletRequest request, HttpServletResponse response, @RequestBody AnInvitation anInvitation)throws Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anInvitation.setInUpdTime(timestamp);

        int i=anInvitationMapper.updateById(anInvitation);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查询帖子")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total=anInvitationMapper.selectCount(null);
        List<AnInvitation> list =anInvitationService.findAll(size,current);
        Result ok = Result.ok().data("AnInvitation",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }
    @ApiOperation(value = "通过发帖人查询帖子")
    @PostMapping("/findByname")
    public void findByname(HttpServletRequest request, HttpServletResponse response,@RequestParam("username") String  username)throws  Exception{
        QueryWrapper wrapper = new QueryWrapper();
        AnUser anUser=anUserMapper.findByname(username);
        if(anUser!=null){
            wrapper.eq("userId",anUser.getUserId());
            List<AnInvitation> list =anInvitationMapper.selectList(wrapper);
            Result ok = Result.ok().data("AnInvitation",list);
            this.WriteJSON(request, response, ok);
        }else{
            Result error=Result.error();
            error.setMessage("查无此人");
            this.WriteJSON(request, response, error);
        }


    }
    @ApiOperation(value = "按帖子id查询帖子")
    @PostMapping("/findById")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("inId") int  inId)throws  Exception{
        AnInvitation list=  anInvitationService.findById(inId);

        if(list!=null){
            Result ok = Result.ok().data("product",list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此帖子");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "按帖子帖子标题模糊查询")
    @PostMapping("/findByInName")
    public void findByIdaaa(HttpServletRequest request, HttpServletResponse response,@RequestParam("anTitle") String   anTitle,@RequestParam("size") int size,@RequestParam("current") int current)throws  Exception{

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("an_title",anTitle);
        List<AnInvitation> list=anInvitationMapper.selectList(queryWrapper);

        if(list!=null){
            Result ok = Result.ok().data("product",list);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有此帖子");
        this.WriteJSON(request, response, error);
    }
}

